package com.qqbot.impl.service.impl;

import java.text.MessageFormat;

import com.alibaba.fastjson.JSONObject;
import com.qqbot.core.message.support.ImageMessage;
import com.qqbot.impl.utils.HttpClientUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.qqbot.core.message.MessageChain;
import com.qqbot.impl.service.DuJiaoShouApiService;
import org.springframework.util.StringUtils;

/**
 * 独角兽API服务
 *
 * @author changwenpeng
 * @date 2022年5月9日
 */
@Slf4j
@Service
public class DuJiaoShouApiServiceImpl implements DuJiaoShouApiService {

    @Value("${mybot.qrUrl}")
    String qrUrl;

    @Value("${mybot.qrImg}")
    String qrImg;

    @Value("${mybot.qrDesc}")
    String qrDesc;

    @Override
    public MessageChain morning() throws Exception {
        String url = "https://ovooa.com/API/image_Tnem?time=morning&QR_str={0}&QR_img={1}&QR_str_ud={2}";
        url = MessageFormat.format(url, qrUrl, qrImg, qrDesc);
        return new MessageChain().text("早安").face("74").image(url);
    }

    @Override
    public MessageChain night() throws Exception {
        String url = "https://ovooa.com/API/image_Tnem?time=night&QR_str={0}&QR_img={1}&QR_str_ud={2}";
        url = MessageFormat.format(url, qrUrl, qrImg, qrDesc);
        return new MessageChain().text("晚安").face("75").image(url);
    }

    @Override
    public MessageChain news() throws Exception {
        String url = "https://api.iyk0.com/60s";
        String resJson = HttpClientUtils.get(url, 10000, 10000);
        JSONObject json = JSONObject.parseObject(resJson);
        String imgUrl = json.getString("imageUrl");
        return new MessageChain().text("新闻早知道！\n").image(imgUrl);
    }

    @Override
    public MessageChain musicCustom(String keyword, String uName) throws Exception {
        String url = "https://ovooa.com/API/QQ_Music/?Skey=&uin=&msg=" + keyword + "&n=1";
        String resJson = HttpClientUtils.get(url, 10000, 10000);
        if (StringUtils.hasText(resJson)) {
            JSONObject json = JSONObject.parseObject(resJson);
            JSONObject data = json.getJSONObject("data");
            String mUrl = data.getString("url");
            String pic = data.getString("picture");
            String title = data.getString("song");
            String content = "歌手简介：" + uName;
            String radio = data.getString("music");
            return new MessageChain().music("custom","qq", mUrl, radio, title, content, pic);
        }
        return new MessageChain();
    }

    @Override
    public MessageChain video(String keyword) throws Exception {
        if ("小姐姐".equals(keyword)) {
            int i = randomId();
            String url = "https://v.api.aa1.cn/api/api-dy-girl/index.php?aa1=url";
            String resText = HttpClientUtils.get(url, 15000, 15000);
            if (StringUtils.hasText(resText)) {
                int index = resText.indexOf("</html>");
                String relUrl = resText.substring(index + 7, resText.length());
                relUrl = relUrl.replaceAll("\n", "");
                relUrl = relUrl.replaceAll(" ","%20");
                log.info("https:" +relUrl);
                return new MessageChain().video("https:" + relUrl);
            }
        }
        String url = "https://ovooa.com/API/sjsp/api.php?msg=" + keyword + "&type=json";
        String resJson = HttpClientUtils.get(url, 15000, 15000);
        JSONObject json = JSONObject.parseObject(resJson);
        JSONObject jsonData = json.getJSONObject("data");
        String url1 = jsonData.getString("url");
        log.info(url1);
        return new MessageChain().video(url1);
    }

    @Override
    public ImageMessage head(String keyword) throws Exception {
        return new ImageMessage("https://ovooa.com/API/pa/api.php?QQ=" + keyword);
    }

    /**
     * 生成随机数
     *
     * @return
     */
    private int randomId() {
        return new java.util.Random().nextBoolean() ? 2 : 1;
    }

}
